<template>
	<u-popup :show="show" :round="10" mode="bottom" @close="close" closeOnClickOverlay>
		<view class="popup-box">
			<view class="displayAli_cen paddingB10px fts16 ftwt">
				{{title}}
				<!-- <view class="close-icon" @click="close"><u-icon name="close" color="#000" size="18"></u-icon></view> -->
			</view>
			<view class="" v-if="isSearch">
				<view class="bor_bottom_f2f2f2 paddingB10px">
					<u-search placeholder="搜索" v-model="Keywords" shape="square" :showAction="false"
						 bgColor="#f5f6fa" searchIconColor="#c4c4c4" searchIconSize="24" color="#000"
						placeholderColor='#999' height='36' @change="changeKey"></u-search>
				</view>
			</view>
			<slot name='selectPay'></slot>
			<scroll-view scroll-y="true" style="max-height:60vh;" @scrolltolower="e=>$emit('scrolltolower',e)">
				<view class="paddingTB15px fts17 text_cen bor_radius4" :class="[item[label]?'':'text_cen', item['disabled']?'c-000opacity45':'c-000']" :style='optionIndex===index?"color:#0049FF;background: rgba(95, 120, 255,.2);font-weight:600;":""' v-for="(item,index) in filterList" :key="index" @click="clickOption(item,index)">
					{{item[label] || item || ''}}
				</view>
				<view v-if="filterList.length==0" class="display juzhong">无数据</view>
			</scroll-view>
			
			<!-- <view class="btn-box" v-if="!isSearch">
				<view @click="close" class="cancel-btn">取消</view>
				<view @click="onConfirm()" class="cancel-btn">确定</view>
			</view> -->
			<view class="displayAli_cen justify-spb mat20" v-if="!isSearch">
				<button class="Btn2 wid45" type="button" @click="close">取消</button>
				<button class="Btn1 wid45" type="button" @click="onConfirm">确定</button>
			</view>
			
			<slot></slot>
			
		</view>
	</u-popup>
</template>

<script>
	export default {
		props: {
			show: Boolean, // 弹窗开关
			title:String, // 标题
			isSearch:{ // 是否常规
				type:Boolean,
				default:()=>false
			},
			list:{ // 列表
				type:Array,
				default:()=>[]
			},
			label:{ // 列表显示字段
				type:String,
				default:()=>''
			},
			isClear:{ // 是否清空选中
				type:Boolean,
				default:()=>false
			},
		},
		data() {
			return {
				Keywords:"",
				filterList:[], // 筛选list
				
				optionValue:"",
				optionIndex:"",
			}
		},
		watch:{
			list(n,o){
				this.filterList=n
				// console.log('监听list',n);
			},
			isClear(n,o){
				console.log(n);
				if(n){
					this.optionValue=""
					this.optionIndex=""
				}
			},
		},
		mounted() {
			this.filterList=this.list
			console.log(this.list);
		},
		methods: {
			changeKey(e) { // 监听列表搜索
				clearTimeout(this.times);
				//当没有频繁点击后，最后执行的定时器
				this.times = setTimeout(() => {
					//里面写执行代码
					console.log(e);
					this.filterList=this.list.filter((i) => (i[this.label] || i).includes(e));
					console.log(this.filterList);
				}, 500)
				
			},
			
			// 点击选项
			clickOption(item,index) {
				this.optionValue=item
				this.optionIndex=index
			},
			
			// 点击确定
			onConfirm() {
				this.$emit("onConfirm",this.optionValue)
				this.optionValue=""
				// this.optionIndex=""
				// this.close()
			},
			close() {
				this.$emit("onClose")
			}
		}
	}
</script>

<style lang="scss" scoped>
	.popup-box {
		padding: 20px 30rpx 50rpx;
		.btn-box {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 30rpx;
			margin-top: 20px;
		
			>view {
				flex: 1;
				text-align: center;
			}
		
			>view:last-child {
				margin-right: 0;
			}
		
			.cancel-btn {
				height: 88rpx;
				margin-right: 20rpx;
				line-height: 88rpx;
				background-color: #f0f1f5;
				border-radius: 8rpx;
			}
		
			.confirm-btn {
				height: 88rpx;
				line-height: 88rpx;
				background-color: #166ffc;
				border-radius: 8rpx;
				color: #fff;
			}
		}
		
	}
</style>